package cn.bb.bbstudy.controller;

import cn.bb.bbstudy.pojo.*;
import cn.bb.bbstudy.service.*;
import cn.bb.bbstudy.utils.BBUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private HttpSession session;
    /**
     * 展示个人中心  需分页
     *
     * @return
     */
    @GetMapping("/user/{uid}")
    public String index(@PathVariable String uid, Model model) {
        //回显数据
        callbackInfo(uid, model);
        //默认一页10条
        PageHelper.startPage(1, 10);
        PageInfo<Blog> info = new PageInfo<>(blogService.findAllByUIdDesc(uid));
        //回显博客信息 因为刚打开显示的就是博客内容
        model.addAttribute("info", info);
        return "user/index";

    }

    @GetMapping("/user/blog/{uid}")
    public String blogTo(@PathVariable String uid, Model model,
                         @RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "limit", defaultValue = "10") int limit
    ) {
        //回显数据
        callbackInfo(uid, model);
        //默认一页10条
        PageHelper.startPage(page, limit);
        PageInfo<Blog> info = new PageInfo<>(blogService.findAllByUIdDesc(uid));
        //回显博客信息 因为刚打开显示的就是博客内容
        model.addAttribute("info", info);
        return "user/index";

    }

    @GetMapping("/user/question/{uid}")
    public String questionTo(@PathVariable String uid, Model model,
                             @RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "limit", defaultValue = "10") int limit
    ) {
        //回显数据
        callbackInfo(uid, model);
        //默认一页10条
        PageHelper.startPage(page, limit);
        PageInfo<Question> info = new PageInfo<>(questionService.findAllByUidDesc(uid));
        //回显问题内容
        model.addAttribute("info", info);
        return "user/user-question";

    }

    @GetMapping("/user/comment/{uid}")
    public String commnetTo(@PathVariable String uid, Model model,
                            @RequestParam(value = "page", defaultValue = "1") int page,
                            @RequestParam(value = "limit", defaultValue = "10") int limit
    ) {
        //回显数据
        callbackInfo(uid, model);
        //默认一页10条
        PageHelper.startPage(page, limit);
        PageInfo<Comment> info = new PageInfo<>(commentService.selectAllByUid(uid));
        //回显回复的内容
        model.addAttribute("info", info);
        return "user/user-comment";

    }

    //根据uid 获取用户信息
    public void callbackInfo(String uid, Model model) {
        //获取用户信息
        UserInfo userInfo = userInfoService.selectInfoByUid(uid);
        model.addAttribute("userInfo", userInfo);
        //如果兴趣栏不为空 也要回显
        String hobby = userInfo.getHobby();
        //不为null 也不为""时
        if (hobby != null && !hobby.isEmpty()) {
            String[] hobbies = hobby.split(",");
            model.addAttribute("hobbies", hobbies);
        }
        //回显 问题数  博客数 回复数
        int qcount = questionService.count(uid);
        int bcount = blogService.count(uid);
        int ccount = commentService.count(uid);
        model.addAttribute("qcount", qcount);
        model.addAttribute("bcount", bcount);
        model.addAttribute("ccount", ccount);
    }


    // 更新头像
    @GetMapping("/user/update-avatar/{uid}")
    public String toUpdateAvatar(@PathVariable String uid, Model model) {
        // 用户信息回填
        callbackInfo(uid, model);
        return "user/update-avatar";
    }

    @PostMapping("/user/updateAvatar/{uid}")
    public String updateAvatar(@PathVariable String uid, MultipartFile multipartFile) {
        //先根据uid查询对应的user对象
        User user = userService.findUserByUid(uid);
        BBUtils.print(user);
        try {
            //返回的是图片在cos里的地址
            String upload = fileUploadService.upload(multipartFile);
            //直接存到数据库了!!!
            user.setAvatar(upload);
            userService.updateByPrimaryKeySelective(user);
            //更新session里的user  实现刷新
            session.setAttribute("loginUser", user);
            return "redirect:/user/update-avatar/" + uid;
        } catch (Exception e) {
            e.printStackTrace();
            return "error/404";
        }
    }
}
